import time

import requests

base_url = "http://10.217.8.8/gms-ths-quoteag-mini"
gms_tech_url = "https://extquote.ainvest.com"

def get_quote_kline(params):
    # 替换为实际的API基础URL
    url = base_url+"/quote/v2/single_kline"
    header = {"Market-Level": "184=level10;168=level10;UDC=level10;"}
    try:
        response = requests.post(url=url, json=params,headers=header,timeout=10)
        response.raise_for_status()  # 检查请求是否成功（状态码 200）
        result_data = response.json()
        if result_data.get("status_code") != 0:
            print.error(f"response is :{result_data},API请求失败: {result_data.get('status_msg')}")
            return []
        return result_data
    except requests.exceptions.HTTPError as errh:
        print("HTTP错误:", errh)
    except requests.exceptions.ConnectionError as errc:
        print("连接错误:", errc)
    except requests.exceptions.Timeout as errt:
        print("超时错误:", errt)
    except requests.exceptions.RequestException as err:
        print("其他异常:", err)

def get_script_indicator(params):
    # url = gms_tech_url+"/gms.indicator.Indicator/v2/GetIndicatorKline"
    url = gms_tech_url+"/indicator/v2/GetIndicatorKline"
    start_time = time.time()  # 记录开始时间
    header = {"Cookie": "userid=1801134343;sessionid=14f7f4a740b9a50a5486bfe20373bd74c",
              "X-Auth-ProgId": "7045",
              "Market-Level": "184=level10;168=level10;UDC=level10;"}
    try:
        response = requests.post(url=url, json=params, headers=header, timeout=10)
        elapsed_time = time.time() - start_time
        print(f"请求url:{url},请求状态：{response.status_code},请求参数：{params} ,请求耗时: {elapsed_time:.4f} 秒")
        response.raise_for_status()  # 检查请求是否成功（状态码 200）
        result_data = response.json()

        if result_data.get("status_code") != 0:
            print.error(f"response is :{result_data},API请求失败: {result_data.get('status_msg')}")
            return None
        return result_data
    except Exception as e:
        print("HTTP请求异常:", e)